import React from 'react';
import PropTypes from 'prop-types';
import {Card, Button, Row, Col, Collapse, Spin} from 'antd';
import {FILES_API, FORM_OPTIONS} from '../constants';
import {conventEnumValueToString, formatAmount} from '../utils';
import DescriptionList from 'ant-design-pro/lib/DescriptionList';
import {PERMISSION, hasPermissions} from './constants';
import {currency} from '../Enum';
import routes from './routes';
import {getDetail} from './api';
import styles from './style.css';
import DetailTable from './DetailTable';
import FormItem from 'antd/lib/form/FormItem';
import {injectIntl, formatMessage} from './intl';
import TextInput from 'Shared/components/TextInput';
const Panel = Collapse.Panel;
const {Description} = DescriptionList;

export class ConfirmPanel extends React.PureComponent {
    state = {
        loading: false,
        goodwillSettlementDetails: [],
        data: {}
    };

    componentDidMount() {
        this.getDetailInfo();
    }

    getDetailInfo = () => {
        this.setState({loading: true});
        getDetail(this.props.id)
            .then(res => {
                if(res.ok)
                    this.setState({
                        loading: false,
                        data: res.data,
                        goodwillSettlementDetails: res.data.goodwillSettlementDetails,
                    });
                else
                    this.setState(({
                        loading: false
                    }));
            });
    }
    handleFilterChange = (value, name) => {
        const data = Object.assign({}, this.state.data, {
            [name]: value
        });
        this.setState({
            data,
            hasOperat: true
        });
    }
    handleUpdateClick = () => {
        this.props.history.push(routes.update.format(this.props.id));
    }
    onSubmit = () => {
        this.setState({
            loading: true,
        });
        this.props.onSubmit(this.state.data).then(() => {
            if(this.success)
                this.props.successReturn();
            if(!this.isunmount)
                this.setState({loading: false});
        });
    }
    render() {
        const operations = [];
        const {permissions, onCancel} = this.props;
        const {data = {}, loading, goodwillSettlementDetails} = this.state;
        const submitable = hasPermissions(PERMISSION.confirm, permissions);
        const packingPanel = goodwillSettlementDetails.map(item => {
            const header = (
                <Row>
                    <Col span={4}>
                        {`${formatMessage({
                            id: 'confirmPanel.info.goodWillCode',
                            defaultMessage: 'GoodWill编号'
                        })}: ${item.goodWillCode}`}
                    </Col>
                    <Col span={4}>
                        {`${formatMessage({
                            id: 'confirmPanel.info.laborFee',
                            defaultMessage: '工时费'
                        })}: ${item.laborFee}`}
                    </Col>
                    <Col span={4}>
                        {`${formatMessage({
                            id: 'confirmPanel.info.materialFee',
                            defaultMessage: '材料费'
                        })}: ${item.materialFee}`}
                    </Col>
                    <Col span={4}>
                        {`${formatMessage({
                            id: 'confirmPanel.info.totalFee',
                            defaultMessage: '费用合计'
                        })}: ${item.totalFee}`}
                    </Col>
                </Row>
            );
            const dataSource = item.goodWillDetails || [];
            return (
                <Panel key={item.id} header={header}>
                    <DetailTable data={dataSource} />
                </Panel>
            );
        });
        if(hasPermissions(permissions, PERMISSION.update) && hasPermissions(data.options, PERMISSION.update))
            operations.push(<Button key="update" onClick={this.handleUpdateClick} size="small">{
                formatMessage({
                    id: 'confirmPanel.btn.update',
                    defaultMessage: '编辑'
                })
            }</Button>);
        return (
            <div className={styles.detail}>
                <Spin spinning={loading}>
                    <Card>
                        <DescriptionList size="large" title={<Row><Col span={12}>{
                            formatMessage({
                                id: 'confirmPanel.card.tradeMethodmessage',
                                defaultMessage: '基本信息'
                            })
                        }</Col>
                        <Col className={`col-align-right ${styles.detail}`} span={12}>{operations}</Col>
                        </Row>}>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.code',
                                defaultMessage: '编号',
                            })}>{data.code}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.brand',
                                defaultMessage: '品牌'
                            })}>{`(${data.brandCode})${data.brandName}`}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.dealer',
                                defaultMessage: '总代',
                            })}>{`(${data.dealerCode})${data.dealerName}`}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.currency',
                                defaultMessage: '币种'
                            })}>{conventEnumValueToString(currency, data.currency)}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.country',
                                defaultMessage: '国家'
                            })}>{data.country}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.laborFee',
                                defaultMessage: '工时费合计',
                            })}>{formatAmount(data.laborFee)}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.materialFee',
                                defaultMessage: '材料费'
                            })}>{formatAmount(data.materialFee)}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.totalFee',
                                defaultMessage: '费用合计'
                            })}>{formatAmount(data.totalFee)}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.comment',
                                defaultMessage: '业务内容'
                            })}>{data.comment}</Description>
                            <Description term={formatMessage({
                                id: 'confirmPanel.data.remark',
                                defaultMessage: '备注'
                            })}>{data.remark}</Description>
                        </DescriptionList>
                    </Card>
                    <Card
                        title={formatMessage({
                            id: 'confirmPanel.details.title',
                            defaultMessage: 'GoodWill结算清单'
                        })}
                        bordered={false}>
                        <Collapse>
                            {packingPanel}
                        </Collapse>
                    </Card>
                    {data.attachments && data.attachments.length >= 0
                        ? <Card><DescriptionList size="large" title={formatMessage({
                            id: 'confirmPanel.card.attachment',
                            defaultMessage: '附件'
                        })}>
                            {data.attachments.map(item => (<Description key={item.fileId}>
                                <a icon="download" download href={`${FILES_API.files}/${item.fileId}`} size="small">
                                    {item.fileName}</a></Description>))}
                        </DescriptionList></Card>
                        : <Card><DescriptionList size="large" title={formatMessage({
                            id: 'confirmPanel.content.noAttachment',
                            defaultMessage: '暂无附件'
                        })}>
                        </DescriptionList></Card>}
                    <Card {...FORM_OPTIONS.col} title={formatMessage({
                        id: 'confirmPanel.card.comment',
                        defaultMessage: '意见'
                    })}>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'confirmPanel.form.uplpadComment',
                                        defaultMessage: '上传意见'
                                    })}
                                    {...FORM_OPTIONS.item} >
                                    {data.uplpadComment}
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'confirmPanel.form.approveComment',
                                        defaultMessage: '确认意见'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextInput
                                        type="textarea"
                                        name="approveComment"
                                        value={data.approveComment}
                                        onBlur={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                        </Row>
                    </Card>
                    <Card type="inner">
                        <Row>
                            <Col disabled={!submitable} className="col-align-right">
                                <Button key="submit" loading={loading}
                                    onClick={this.onSubmit} type="primary">{formatMessage({
                                        id: 'confirmPanel.btn.submit',
                                        defaultMessage: '保存'
                                    })}</Button>
                            </Col>
                        </Row>
                    </Card>
                </Spin>
                <div className="page-toolbar">
                    <Button
                        type="primary"
                        disabled={loading}
                        onClick={onCancel}>{formatMessage({
                            id: 'confirmPanel.btn.return',
                            defaultMessage: '返回'
                        })}</Button>
                </div>
            </div>
           
        );
    }
}

ConfirmPanel.propTypes = {
    permissions: PropTypes.array.isRequired,
    onCancel: PropTypes.func.isRequired,
    history: PropTypes.object,
    successReturn: PropTypes.func,
};

import {connect} from 'react-redux';
import {onConfirm, tableSearch} from './actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = (state, props) => ({
    id: props.id,
    permissions: getPermissions(state),
});
const mapDispatchToProps = (dispatch, props) => ({
    onSubmit: data => dispatch(onConfirm(data)).then(result => {
        if(result.success) {
            dispatch(tableSearch());
            props.history.push(routes.query.url());
        }
    }),
    onCancel: () => {
        props.history.push(routes.query.url());
    },
    successReturn: () => {
        dispatch(tableSearch());
        props.history.push(routes.query.url());
    },
});
export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(ConfirmPanel));
